package org.ccay.web.support.servlet.util;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.ccay.core.exception.ExceptionHandler;
import org.ccay.core.exception.FaultVO;
import org.ccay.core.util.HttpUtil;
import org.ccay.web.support.filter.ResponseUtil;
import org.ccay.web.support.internal.impl.RequestUtil;

/**
 * Servlet 异常处理	
 */
public final class ServletExceptionHandler {
	/**
	 * 防止创建实例
	 */
	private ServletExceptionHandler(){
	}
	
	
	/** 
	 * 处理异常
	 * @param ex
	 * @param request
	 * @param response
	 */
	public static void handle(Throwable ex,HttpServletRequest request,HttpServletResponse response) {		
		ex = refind(ex);
		
		ExceptionHandler.logException(ex);
		
		FaultVO fault = ExceptionHandler.getFriendlyFault(ex, false);
		
		handleHttpFault(request, response, fault);
	}

	/** 
	 * 处理Http错误
	 * @param request
	 * @param response
	 * @param fault
	 */
	private static void handleHttpFault(HttpServletRequest request, HttpServletResponse response, FaultVO fault) {
		if(fault.getHttpCode() == HttpUtil.NOT_LOGIN){
			processNotLogin(request,response,fault);
			return ;
		}
		//默认423错误
		if(fault.getHttpCode() == HttpUtil.MAINTENANCE){
			processMaintenance(request,response);
			return ;
		} 
		//默认500错误
		if(fault.getHttpCode() == 0){
			fault.setHttpCode(HttpUtil.SERVER_ERROR);
		} 
		//AJAX请求，发送响应码
		if(RequestUtil.isAjaxRequest(request) || !RequestUtil.isHtmlResponse(request, response)){
			ResponseUtil.sendError2Response(response,fault.getHttpCode(),fault);
			return;
		}
		//发送错误页面
		ResponseUtil.sendErrorPage(request, response, fault);
	}

	/** 
	 * 重新寻找异常
	 * @param ex
	 * @return
	 */
	private static Throwable refind(Throwable ex) {
		//挖掘Ccay异常（刨去上层无用异常）
		if(ex instanceof ServletException && ((ServletException)ex).getRootCause()!=null) {
			ex = ((ServletException)ex).getRootCause();
		} 
		ex = ExceptionHandler.digException(ex);
		return ex;
	}
	
	/** 
	 * 处理系统变更情况
	 * @param request
	 * @param response
	 */
	private static void processMaintenance(HttpServletRequest request, HttpServletResponse response) {		
		try {
			response.sendRedirect(RequestUtil.getContextPath(request) + "har/maintenance.html");
		} catch (IOException e) {
			ExceptionHandler.logException(e);
		}
	} 
	
	/** 
	 * 处理未登录情况
	 * @param request
	 * @param response
	 */
	private static void processNotLogin(HttpServletRequest request, HttpServletResponse response, FaultVO fault ) {	
		//TODO 处理未登录情况
//		if( RequestUtil.isSessionRebuildPage(request)){
//			//最后True表示打开小的重建窗口
//			//传递URL中的参数 pwx373895 2016-10-12
//			String paramString = request.getQueryString();
//			paramString = !StringUtil.isNullOrEmpty(paramString) ? "?"+paramString : "";
//			ssoProvider.redirect2Login(request, response, request.getRequestURL().toString()+paramString, true);	
//		}else if(RequestUtil.isHomePage(request) ){
//			ssoProvider.redirect2Login(request, response, request.getRequestURL().toString(), false);
//		}else if(RequestUtil.isAjaxRequest(request) ){
//			ResponseUtil.sendError2Response(response,fault.getHttpCode(),fault);
//		}else{
//			ssoProvider.redirect2Login(request, response, request.getRequestURL().toString(), false);
//		}
	} 
 }
